﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COUP.Core.Networking
{
    public unsafe static class Spliter
    {


        public static void Split(ClientState State, byte[] Data)
        {
            State.Cryptographer.Decrypt(ref Data);

            byte[] newData = Data;
            byte[] newPacket;
            int Length = 0, Count = 0;

            while (Length <= newData.Length)
            {
                fixed (byte* PTR = newData)
                    Length = (ushort)*(ushort*)PTR;

                Count += Length;
                newPacket = new byte[Length];
                Buffer.BlockCopy(newData, 0, newPacket, 0, Length);
                newData = new byte[newData.Length - Length];
                if(newData.Length > 0)
                    Buffer.BlockCopy(Data, Count, newData, 0, newData.Length);
                Packet Temp = new Packet(newPacket);

                Handle.Invoke(State, Temp);
            }
        }
    }
}
